package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {

    @Autowired
    private DishService dishService;
    private Dish dish;

    // 菜品分页查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, String name) { // 1.接收参数

        // 2.调用service
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);

        // 3.返回结果
        return ResultInfo.success(page);

    }

    //菜品回显
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        //1.接收菜品id
        //2.调用service查询
        Dish dish = dishService.findById(id);
        //3.返回结果
        return ResultInfo.success(dish);
    }
    //新增菜品 Request URL: http://localhost:8080/dish
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish ) {
        //接收参数
        //2.调用service保存
        dishService.save(dish);
        //3.返回结果
        return ResultInfo.success(null);
    }
    //菜品修改
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        this.dish = dish;
        //1.接收请求参数
        //2.调用service修改
        dishService.update(dish);
        //3.返回消息
        return ResultInfo.success(null);

    }




//删除
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids) {
        dishService.deleteBatchIds(ids);
        return ResultInfo.success(null);
    }
//停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        dishService.updateStatus(status,ids);
        return ResultInfo.success(null);
    }

    //根据分类id和type=1查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId, @RequestParam(defaultValue = "1") Integer status, String name) {

        List<Dish> dishList = dishService.findList(categoryId,status,name);
        return ResultInfo.success(dishList);
    }


}